package com.ly.springboot.flter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      HttpServletRequest request  =(HttpServletRequest)servletRequest;
      HttpServletResponse response  =(HttpServletResponse)servletResponse;

      Object loginUser=request.getSession().getAttribute("loginUser");
      if (loginUser != null){
          response.setContentType("text/html;charset=UTF-8");
          PrintWriter out = response.getWriter() ;
          out.print("<script>alert('请先进行登录，再进行后操作!(Filter控制)');location.href='"+request.getContextPath()+"';</script>");
          return;
      }
      filterChain.doFilter(request, response);
    }
}
